/**
 * 
 */
var _poolName="";
var _lunName="";
var series="";
var _type = new Array("readio","writeio","readByte","writeByte");
var seriesArr;
var realyType;   // 实时展示 OR 查看历史
var begintime = "";  
var endtime = "";
var _selectType;  // 所选查看类型
var _lastTime ;
var lunio = {
	// 4个实时指标计时器
	timer_readio: null, 
	timer_writeio: null, 
	timer_readByte: null,
	timer_writeByte: null,
	

	clearTimer : function() {
		if (lunio.timer_readio) clearInterval(lunio.timer_readio);
		if (lunio.timer_writeio) clearInterval(lunio.timer_writeio);
		if (lunio.timer_readByte) clearInterval(lunio.timer_readByte);
		if (lunio.timer_writeByte) clearInterval(lunio.timer_writeByte);
	},
	
	readio : function(){
		var lunid = $('#lunio_lunid').val();
		if(null == lunid || "" == lunid){
			return ;
		}
		lunio.clearTimer();
/*		if("" != _lunName){
			lunio.setRealType();
		}*/
		
		_selectType = 0;
		_poolName =  $('#lunio_poolName').val();
		_lunName = $('#lunio_lunName').val();
		$.ajax({
			type : "get",
			url : baseURL + "/image/lunio/initChar",
			dataType : "json",
			data : {poolName : _poolName , name : _lunName , type : _type[0] , begintime : begintime, endtime : endtime},
			success :function(json) {
				_lastTime = json.categories[json.categories.length-1];
			    $('#readio').highcharts({
			    	chart: {
			    		type: 'spline',
			            animation: Highcharts.svg, // don't animate in old IE
			            marginRight: 10,
			            events: {
			                load: function () {
			                    // set up the updating of the chart each 5 seconds
//			                	if (!isReal) return;
			                    seriesArr = this.series;
			                    lunio.timer_readio = setInterval(function () {
			                    	lunio.update(_type[0]);
			                    }, 10000);
			                }
			            }
			    	},
			        title: {
			            text: localeMsg.lun.readio,
			            x: -20 //center
			        },
			        xAxis: {//横坐标
			            categories: json.categories,
			            tickInterval: 60
			        },
			        yAxis: {//纵坐标
			            title: {
			                text: localeMsg.lun.iops
			            },
			            plotLines: [{
			                value: 0,
			                width: 1,
			                color: '#808080'
			            }]
			        },
			        series: json.dataset
			    });
			}
		});
	},
	
	writeio : function(){
		lunio.clearTimer();
		//lunio.setRealType();
		_selectType = 1;
		$.ajax({
			type : "get",
			url : baseURL + "/image/lunio/initChar",
			dataType : "json",
			data : {poolName : _poolName , name : _lunName , type : _type[1], begintime : begintime, endtime : endtime},
			success :function(json) {
				_lastTime = json.categories[json.categories.length-1];
			    $('#writeio').highcharts({
			    	chart: {
			    		type: 'spline',
			            animation: Highcharts.svg, // don't animate in old IE
			            marginRight: 10,
			            events: {
			                load: function () {
			                    // set up the updating of the chart each 5 seconds
//			                	if (!isReal) return;
			                    seriesArr = this.series;
			                    lunio.timer_writeio = setInterval(function () {
			                    	lunio.update(_type[1]);
			                    }, 10000);
			                }
			            }
			    	},
			        title: {
			            text: localeMsg.lun.writeio,
			            x: -20 //center
			        },
			        xAxis: {//横坐标
			            categories: json.categories,
			            tickInterval: 60
			        },
			        yAxis: {//纵坐标
			            title: {
			                text: localeMsg.lun.iops
			            },
			            plotLines: [{
			                value: 0,
			                width: 1,
			                color: '#808080'
			            }]
			        },
			        series: json.dataset
			    });
			}
		});
	},
	
	readByte : function(){
		lunio.clearTimer();
		//lunio.setRealType();
		_selectType = 2;
		$.ajax({
			type : "get",
			url : baseURL + "/image/lunio/initChar",
			dataType : "json",
			data : {poolName : _poolName , name : _lunName , type : _type[2], begintime : begintime, endtime : endtime},
			success :function(json) {
				_lastTime = json.categories[json.categories.length-1];
			    $('#readByte').highcharts({
			    	chart: {
			    		type: 'spline',
			            animation: Highcharts.svg, // don't animate in old IE
			            marginRight: 10,
			            events: {
			                load: function () {
			                    // set up the updating of the chart each 5 seconds
//			                	if (!isReal) return;
			                    seriesArr = this.series;
			                    lunio.timer_writeio = setInterval(function () {
			                    	lunio.update(_type[2]);
			                    }, 10000);
			                }
			            }
			    	},
			        title: {
			            text: localeMsg.lun.readByte,
			            x: -20 //center
			        },
			        xAxis: {//横坐标
			            categories: json.categories,
			            tickInterval: 60
			        },
			        yAxis: {//纵坐标
			            title: {
			                text: localeMsg.lun.byteBp
			            },
			            plotLines: [{
			                value: 0,
			                width: 1,
			                color: '#808080'
			            }]
			        },
			        series: json.dataset
			    });
			}
		});
	},
	
	
	writeByte : function(){
		lunio.clearTimer();
		//lunio.setRealType();
		_selectType = 3;
		$.ajax({
			type : "get",
			url : baseURL + "/image/lunio/initChar",
			dataType : "json",
			data : {poolName : _poolName , name : _lunName , type : _type[3] , begintime : begintime, endtime : endtime},
			success :function(json) {
				_lastTime = json.categories[json.categories.length-1];
			    $('#writeByte').highcharts({
			    	chart: {
			    		type: 'spline',
			            animation: Highcharts.svg, // don't animate in old IE
			            marginRight: 10,
			            events: {
			                load: function () {
			                    // set up the updating of the chart each 5 seconds
//			                	if (!isReal) return;
			                    seriesArr = this.series;
			                    if(realyType){
			                    	lunio.timer_writeio = setInterval(function () {
			                    		lunio.update(_type[3]);
				                    }, 10000);
			                    }
			                }
			            }
			    	},
			        title: {
			            text: localeMsg.lun.writeByte,
			            x: -20 //center
			        },
			        xAxis: {//横坐标
			            categories: json.categories,
			            tickInterval: 60
			        },
			        yAxis: {//纵坐标
			            title: {
			                text: localeMsg.lun.byteBp
			            },
			            plotLines: [{
			                value: 0,
			                width: 1,
			                color: '#808080'
			            }]
			        },
			        series: json.dataset
			    });
			}
		});
	},
	
	filtration : function(){
		switch (_selectType){
		case 0:
			lunio.readio();
			break;
		case 1:
			lunio.writeio();
			break;
		case 2:
			lunio.readByte();
			break;
		case 3:
			lunio.writeByte();
			break;
		default :
			lunio.readio();
		break;
		}
	},
	
	update : function(value){
    	$.ajax({
    		type : "get",
    		url : baseURL + "/image/lunio/update",
    		data :{poolName : _poolName , name : _lunName , type : value , lastTime : _lastTime},
    		dataType : "json",
    		beforeSend : function() {},
    		complete : function() {},
    		success :function(json) {
    			if (!json || !json.dataset || 0 == json.dataset.length ) return;
    			var updateData = json.dataset;
    			var xNames = json.categories;
            	for (var i = 0; i < seriesArr.length; i++) {
                    series = seriesArr[i];
                    var x = xNames[i];
                    if(null != x && "" != x){
                    	_lastTime = x;
                    }
                    var yDatas = updateData[i].data;
                    for (var j = 0; j < yDatas.length; j++) {
                        var y = yDatas[j];
                        series.addPoint([x,y],true,true);
                    }
            	}
    		}
    	});
	},
	
	setRealType : function(){
		begintime = $('#begintime').datetimebox('getValue');
		endtime = $('#endtime').datetimebox('getValue');
		if (begintime == "" && endtime == ""){
			realyType = true;
		}
		else{
			realyType = false;
		}
	},
};
